<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Delete, GetData } from '@/api/modules/sysshareconfig'
import GetCacheData from '@/store/modules/NewPageCache'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const cacheData = GetCacheData()
let PageNum = cacheData.GetCacheData('sysshareconfig').currentPage

const findFrom = ref({
  ...(cacheData.GetCacheData('sysshareconfig').fromData),
})

const dataList: any = reactive({
  list: [],
})
const loading = ref(true)
const total = ref(0)

const actTypeOptionList = [
  {
    value: 1,
    label: '连连看',
  },
]

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.ActId !== '~' && ({ ActId: findFrom.value.ActId })),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList(1, cacheData.GetCacheData('sysshareconfig').currentSize)
  cacheData.SetCacheData('sysshareconfig', PageNum, cacheData.GetCacheData('sysshareconfig').currentSize, findFrom.value)
}

// 添加
function AddData() {
  if (router.currentRoute.value.fullPath === '/skincheckLlksent/sysshareconfig') {
    router.push({
      name: 'skincheckLlksentSysshareconfigDetail',
      query: {
        actId: 1,
      },
    })
  }
  else {
    router.push({
      name: 'sysshareconfigDetail',
    })
  }
}

// 编辑
function Redact(id: number) {
  if (router.currentRoute.value.fullPath === '/skincheckLlksent/sysshareconfig') {
    router.push({
      name: 'skincheckLlksentSysshareconfigDetail',
      query: {
        id,
        actId: 1,
      },
    })
  }
  else {
    router.push({
      name: 'sysshareconfigDetail',
      query: {
        id,
      },
    })
  }
}

// 删除
function DeteleData(id: number) {
  ElMessageBox.confirm(
    '你确定要删除该配置吗？',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      Delete(id).then((res: any) => {
        if (res.statusCode === 200) {
          ElMessage.success('删除成功')
          GetDataList(PageNum, cacheData.GetCacheData('sysshareconfig').currentSize)
        }
        else {
          ElMessage.error(res.error)
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消',
      })
    })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('sysshareconfig', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('sysshareconfig').currentSize)
  cacheData.SetCacheData('sysshareconfig', val, cacheData.GetCacheData('sysshareconfig').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('sysshareconfig').currentPage || 1
})

onMounted(async () => {
  if (router.currentRoute.value.fullPath === '/skincheckLlksent/sysshareconfig') {
    findFrom.value.ActId = 1
  }
  GetDataList(cacheData.GetCacheData('sysshareconfig').currentPage, cacheData.GetCacheData('sysshareconfig').currentSize)
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item v-if="router.currentRoute.value.fullPath !== '/skincheckLlksent/sysshareconfig'" label="活动">
                <el-select v-model="findFrom.ActId" placeholder="请选择活动">
                  <el-option label="全部" value="~" />
                  <el-option v-for="item in actTypeOptionList" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </div>
          </el-form>
          <div v-if="router.currentRoute.value.fullPath !== '/skincheckLlksent/sysshareconfig'" class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="actId" label="活动" width="100">
          <template #default="scope">
            <span>
              {{ actTypeOptionList.find((item: any) => item.value === scope.row.actId)?.label }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="shareImg" label="分享图" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.shareImg" />
          </template>
        </el-table-column>
        <el-table-column header-align="center" prop="shareTxt" label="分享语" min-width="250" />
        <el-table-column align="center" prop="addTime" label="添加时间" width="250" />
        <el-table-column align="center" prop="updateTime" label="修改时间" width="250" />
        <el-table-column align="center" label="操作" fixed="right" min-width="150">
          <template #default="scope">
            <el-button type="primary" @click="Redact(scope.row.id)">
              编辑
            </el-button>
            <el-button type="danger" @click="DeteleData(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('sysshareconfig').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;

  div {
    text-align: left;
  }
}
</style>
